package cn.com.twoke.easyproject.config.thread_pool;

import cn.com.twoke.easyproject.common.context.UserLoginContext;
import cn.com.twoke.easyproject.common.context.holder.UserLoginContextHolder;
import org.springframework.core.task.TaskDecorator;
import org.springframework.stereotype.Component;

/**
 * 用户登录信息上下文任务装饰器
 */
@Component
public class UserLoginContextTaskDecorator implements TaskDecorator {
    @Override
    public Runnable decorate(Runnable runnable) {
        UserLoginContext loginContext = UserLoginContextHolder.get();
        return () -> {
            UserLoginContextHolder.set(loginContext);
            try {
                runnable.run();
            } finally {
                UserLoginContextHolder.clear();
            }
        };
    }
}
